﻿--[[
   @brief    main dialog view
   @author   hqzheng
   @version  1.0
   @date     2017-08-10
--]]


social_main_dialog_view = 
{
    dialog_ptr_ = nil;
    shadow_ptr = nil;
    root_id = 0;
    panel_id = 0;
    title_txt_id = 0;
    last_panel_show_state = false;      -- 记录用户的主动打开/关闭行为

    tab_type = 0;
    init_rc = {}; --初始size

    first_show = true;

    selected_flag_id = 0;
    selected_panel_id = 0;

    tab_info = {};

    SESSION_TAB = 0;
    CONTACT_TAB = 1;

    first_click_contact_tab = true;

    init = function ()
        base_utility.log("[social_main_dialog_view] init ");
    end;

    create = function ()
        if social_main_dialog_view.dialog_ptr_ then
            return true;
        end

        social_main_dialog_view.root_id = layout_mgr.restore_layout("wegame_social\\WeGameSocialDlg.tpf", true);

        if social_main_dialog_view.root_id ~= 0 then
            social_main_dialog_view.dialog_ptr_ = ui_utility.get_ctrl_ptr_by_id(social_main_dialog_view.root_id);
            social_main_dialog_view.init_rc = social_main_dialog_view.dialog_ptr_:GetRectEx(FALSE,TRUE);
            social_main_dialog_view.panel_id = ui_utility.get_ctrl_id_by_path(social_main_dialog_view.root_id, "Cnt_Tabs\\Ctn_MainFrameTab");
            social_main_dialog_view.title_txt_id = ui_utility.get_ctrl_id_by_path(social_main_dialog_view.root_id, "TitleBarCtn\\TitleText");
            social_main_dialog_view.selected_panel_id = ui_utility.get_ctrl_id_by_path(social_main_dialog_view.panel_id, "pnlSelected");
            social_main_dialog_view.selected_flag_id = ui_utility.get_ctrl_id_by_path(social_main_dialog_view.selected_panel_id, "tabSelected");
            if base_utility.is_type_of_oversea_ver() then
                ui_utility.setproperty_by_id(social_main_dialog_view.selected_flag_id, "Background", "COLOR:RGB:25,178,255");
            end
            local shadow_id = layout_mgr.restore_layout("wegame_social\\Shadow.tpf");
            social_main_dialog_view.shadow_ptr = ui_utility.get_ctrl_ptr_by_id(shadow_id);
            ui_utility.setproperty_by_id(shadow_id, "Width", "282");
            ui_utility.setproperty_by_id(shadow_id, "Height", "592");

            ui_utility.setproperty_by_id(social_main_dialog_view.root_id, "OnShow", "social_main_dialog_view.on_wnd_show");
            ui_utility.setproperty_by_id(social_main_dialog_view.root_id, "OnHide", "social_main_dialog_view.on_wnd_hide");
            ui_utility.setproperty_by_id(social_main_dialog_view.root_id, "OnPosChanged", "social_main_dialog_view.on_wnd_move");
            social_main_dialog_view.tab_info[social_main_dialog_view.SESSION_TAB] = {tab_offset_x = 51, tab_offset_y = 0};
            social_main_dialog_view.tab_info[social_main_dialog_view.CONTACT_TAB] = {tab_offset_x = 165, tab_offset_y = 0};
            return true;
        else
            base_utility.log("[social_main_dialog_view] social_main_dialog_view create error!!!");
            return false;
        end
    end;

    show = function (is_show)
        if true == is_show then
            if true == social_main_dialog_view.create() and nil ~= social_main_dialog_view.dialog_ptr_ then
                if "" ~= social_main_dialog_control.nick_name then
                    ui_utility.setproperty_by_id(social_main_dialog_view.title_txt_id, "Caption", social_main_dialog_control.nick_name);
                end
                social_main_dialog_view.dialog_ptr_:Show(TRUE);
                social_main_dialog_view.dialog_ptr_:SetFocus();
                social_main_dialog_view.dialog_ptr_:Restore();
                social_main_dialog_view.dialog_ptr_:BringToTop();
            end
        else
            if nil ~= social_main_dialog_view.dialog_ptr_ then
                social_main_dialog_view.dialog_ptr_:Show(FALSE);
            end
            if nil ~= social_main_dialog_view.shadow_ptr then
                social_main_dialog_view.shadow_ptr:Show(FALSE);
            end
        end
        local data = {main_dialog_id = social_main_dialog_view.root_id};
        comm_center.send_broadcast('i_broadcast_social_mainwnd_show', data);

        if true == social_main_dialog_view.first_show then
            -- 默认打开联系人tab
            social_main_dialog_view.set_tab_focus(1);
            -- 展开好友列表
            comm_center.call_service('i_service_social_pannel_open_friend_list', {});
            social_main_dialog_view.first_show = false;
        end
    end;

    on_session_btn_signaled = function(uilib, ctrl_id)
        comm_center.send_broadcast('i_social_broadcast_session_btn_signaled', {});
    end;

    on_session_btn_unsignaled = function(uilib, ctrl_id)
        comm_center.send_broadcast('i_social_broadcast_session_btn_unsignaled', {});
    end;

    on_contact_btn_signaled = function(uilib, ctrl_id)
        comm_center.send_broadcast('i_social_broadcast_contact_btn_signaled', {});
    end;

    on_contact_btn_unsignaled = function(uilib, ctrl_id)
        comm_center.send_broadcast('i_social_broadcast_contact_btn_unsignaled', {});
    end;

    button_contact_clicked = function(uilib, dwID_Click)
        if social_main_dialog_view.tab_type == social_main_dialog_view.CONTACT_TAB then
            return;
        end
        if true == social_main_dialog_view.first_click_contact_tab then
            social_main_dialog_view.move_select_flag(social_main_dialog_view.CONTACT_TAB);
            social_main_dialog_view.first_click_contact_tab = false;
        else
            social_main_dialog_view.move_select_flag(social_main_dialog_view.CONTACT_TAB, true);
        end
        
        social_main_dialog_view.tab_type = social_main_dialog_view.CONTACT_TAB;
        event_center.send_event(social_event.CLICK_SOCIAL_CONTACT_TAB_BTN, base_utility.table_to_json({contact_tab_id = dwID_Click}));

        social_tips_control.show_group_guide_tips(FALSE);

        comm_center.call_service("i_service_tqos_group_tab_click", {});  -- 这是老的统计，应该要废弃了
        comm_center.call_service("i_service_increase_social_panel_contact_tabl_show_times", {});

    end;

    button_session_clicked = function(uilib, dwID_Click)
        social_network_model.msg_tab_has_clicked_and_no_new_msg_from_server_ = true;
        if social_main_dialog_view.tab_type == social_main_dialog_view.SESSION_TAB then
            return;
        end
        social_main_dialog_view.move_select_flag(social_main_dialog_view.SESSION_TAB, true);
        social_main_dialog_view.tab_type = social_main_dialog_view.SESSION_TAB;
        event_center.send_event(social_event.CLICK_SOCIAL_SESSION_TAB_BTN, "{}");

        social_tips_control.show_friend_guide_tips(FALSE);

        comm_center.call_service("i_service_tqos_friend_tab_click", {});  -- 这是老的统计，应该要废弃了
        comm_center.call_service("i_service_increase_social_panel_msg_tabl_show_times", {});

        social_network_model.group_tab_reddot_status = false;
        social_main_dialog_view.show_group_new_message_red_point_impl();  
    end;

    get_rc = function ()
        if social_main_dialog_view.dialog_ptr_ ~= nil then
            local new_rc = social_main_dialog_view.dialog_ptr_:GetRectEx(FALSE,TRUE);
            return social_main_dialog_view.init_rc, new_rc;
        end
        return nil, nil;
    end;

    close = function (uilib, ctrl_id)
        social_main_dialog_view.show(false);
    end;

    min = function (uilib, control_id)
        if nil ~= social_main_dialog_view.dialog_ptr_ then
            social_main_dialog_view.dialog_ptr_:Minimize();
        end
    end;

    is_show = function ()
        local is_show = FALSE;
        if social_main_dialog_view.dialog_ptr_ ~= nil then
        is_show = social_main_dialog_view.dialog_ptr_:IsVisible();
        end
        return is_show;
    end;

    ------社交相关UI动作--------------------------------------------------------------
    show_group_new_message_red_point = function(event_id,json_str)
        local message = base_utility.json_to_table(json_str);

        if message == nil or message.show == nil then
            return;
        end

        if message.show == "true" then
            social_network_model.group_tab_reddot_status = true;
        else
            social_network_model.group_tab_reddot_status = false;
        end

        social_main_dialog_view.show_group_new_message_red_point_impl();
    end;

    show_group_new_message_red_point_impl = function()
        if social_network_model.group_tab_reddot_status == true and social_main_dialog_view.tab_type == social_main_dialog_view.SESSION_TAB then
            social_network_model.group_tab_reddot_status = false;
        end

        local reddot_id = ui_utility.get_ctrl_id_by_path(social_main_dialog_view.panel_id,
            "ButtonGroup_Tab\\Btn_SessionPnl\\rect\\TinyButton_Little_Red_Friend");

        if reddot_id == 0 then
            return;
        end
        if social_network_model.group_tab_reddot_status == true then
            ui_utility.setproperty_by_id(reddot_id, "Visible", "true");
        else
            ui_utility.setproperty_by_id(reddot_id, "Visible", "false");
        end
    end;

    show_friend_new_message_red_point = function(event_id,json_str)
        local message = base_utility.json_to_table(json_str);

        if message == nil or message.show == nil then
            return;
        end

        if message.show == "true" then
            social_network_model.group_tab_reddot_status = true;
        else
            social_network_model.group_tab_reddot_status = false;
        end

        social_main_dialog_view.show_group_new_message_red_point_impl();
    end;

    set_tab_focus = function (tab_type)
        local tab_exclude_ctrl_ptr = ui_utility.get_ctrl_ptr_by_path(social_main_dialog_view.panel_id, "ButtonGroup_Tab\\Excluder_ForTabSwitch");
        local tab_exclude_ctrl_ptr = tolua.cast(
                tab_exclude_ctrl_ptr, "Tenio::IUIExcluder");
        if nil ~= tab_exclude_ctrl_ptr then
            tab_exclude_ctrl_ptr:SetCheckedIndex(tab_type);
            if 0 == tab_type then
                local tab_id = ui_utility.get_ctrl_id_by_path(social_main_dialog_view.panel_id, "ButtonGroup_Tab\\Btn_SessionPnl");
                social_main_dialog_view.button_session_clicked(ui_utility.uilib(), tab_id);
            elseif 1 == tab_type then
                local tab_id = ui_utility.get_ctrl_id_by_path(social_main_dialog_view.panel_id, "ButtonGroup_Tab\\Btn_ContactPnl");
                social_main_dialog_view.button_contact_clicked(ui_utility.uilib(), tab_id);
            end
        end
    end;

   -- 窗口显示时处理
    on_wnd_show = function()
        -- 解决无法置顶的问题
        local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                        "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= social_main_dialog_view.dialog_ptr_ and nil ~= pTPFUI then
            if nil ~= social_main_dialog_view.shadow_ptr then
                social_main_dialog_view.shadow_ptr:Show(TRUE);
                -- social_main_dialog_view.shadow_ptr:BringToTop();
            end;

            local HWND_TOPMOST = pTPFUI:number_to_hwnd(-1);
            local SWP_NOMOVE = 0x002;
            local SWP_NOSIZE = 0x001;

            local dwFlags = SWP_NOMOVE + SWP_NOSIZE;

            -- social_main_dialog_view.dialog_ptr_:SetWindowPos(HWND_TOPMOST, 0, 0, 0, 0, dwFlags);
            -- social_main_dialog_view.dialog_ptr_:MoveToCenter();
        end;
    end;

    -- 窗口隐藏
    on_wnd_hide = function()
        social_main_dialog_view.show(false);
    end;

    on_wnd_move = function()
        if nil ~= social_main_dialog_view.shadow_ptr and nil ~= social_main_dialog_view.dialog_ptr_ then
            local     rtThis = social_main_dialog_view.dialog_ptr_:GetRectEx(TRUE,TRUE);
            local     rtTemp = social_main_dialog_view.shadow_ptr:GetRectEx(TRUE,TRUE);
            local     nWidth = rtThis.right - rtThis.left + 32;
            local     nHeight= rtThis.bottom - rtThis.top + 32;

            rtTemp.left  = rtThis.left - 16;
            rtTemp.top   = rtThis.top  - 6;
            rtTemp.right = rtTemp.left + nWidth;
            rtTemp.bottom= rtTemp.top  + nHeight;

            social_main_dialog_view.shadow_ptr:MoveEx(rtTemp, TRUE);
            event_center.send_event(social_event.SOCIAL_PANEL_POSITION_CHANGED, "{}");
        end;
    end;

    set_title_name = function (name)
        if 0 ~= social_main_dialog_view.title_txt_id and nil ~= name and "" ~= name then
            ui_utility.setproperty_by_id(social_main_dialog_view.title_txt_id, "Caption", name);
        end
    end;

    cal_tab_offset = function (tab_type)
        local info = social_main_dialog_view.tab_info[tab_type];
        if nil == info then
            return 0, 0; 
        end
        local offset_x = info.tab_offset_x;
        local offset_y = info.tab_offset_y;
        if nil == offset_x then
            offset_x = 0;
        end
        if nil == offset_y then
            offset_y = 0;
        end
        return offset_x, offset_y;
    end;

    move_select_flag = function (dst_tab_type, is_use_animation)
        local select_flag_ptr = ui_utility.get_ctrl_ptr_by_id(social_main_dialog_view.selected_flag_id);
        if nil == select_flag_ptr then
            return;
        end

        local select_flag_rc = select_flag_ptr:GetRectEx(TRUE,TRUE);
       
        local select_flag_w = select_flag_rc.right - select_flag_rc.left;
        local select_flag_h = select_flag_rc.bottom - select_flag_rc.top;
        local cur_tab_offset_x, cur_tab_offset_y = social_main_dialog_view.cal_tab_offset(social_main_dialog_view.tab_type);
        local dst_tab_offset_x, dst_tab_offset_y = social_main_dialog_view.cal_tab_offset(dst_tab_type);

        if true == is_use_animation then
            -- 位移动画
            local start_pos = POINT:new_local();
            start_pos.x = cur_tab_offset_x;
            start_pos.y = cur_tab_offset_y;

            local end_pos = POINT:new_local();
            end_pos.x = dst_tab_offset_x;
            end_pos.y = dst_tab_offset_y;

            animation_utility.init(
                social_main_dialog_view.selected_flag_id,
                "Select_Flag_Move", animation_utility.ANI_TYPE.ANI_MOVE);
            animation_utility.move_animation(
                social_main_dialog_view.selected_flag_id, start_pos, end_pos, 8, 0);
        else
            select_flag_rc.left = dst_tab_offset_x;
            select_flag_rc.right = select_flag_rc.left + select_flag_w;
            select_flag_rc.top = dst_tab_offset_y;
            select_flag_rc.bottom = select_flag_rc.top + select_flag_h;
            select_flag_ptr:MoveEx(select_flag_rc,TRUE);
        end
    end;
};

social_main_dialog_view.init();

